package cn.monkey.family.data.crud.repository;

import cn.monkey.family.data.QueryRequest;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.ListQueryByExampleExecutor;

import java.util.List;
import java.util.Optional;

public interface ExampleQueryRequestRepository<T, ID, Q extends QueryRequest>
        extends QueryRequestRepository<T, ID, Q>, ListQueryByExampleExecutor<T> {

    Example<T> example(Q queryRequest);

    @Override
    default List<T> findAllByQueryRequest(Q queryRequest, Sort sort) {
        return this.findAll(example(queryRequest), sort);
    }

    @Override
    default Page<T> findPageByQueryRequest(Q queryRequest, Pageable pageable) {
        return this.findAll(example(queryRequest), pageable);
    }

    default Optional<T> findOneByQueryRequest(Q queryRequest) {
        return this.findOne(example(queryRequest));
    }
}
